import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

/**
 * MovieCollection
 */
public class Support {

    private static WebDriver driver;

    public static void init(String url) {
        driver = new ChromeDriver();
        driver.get(url);
        driver.manage().window().maximize();
    }

    public static void close() {
        driver.quit();
    }

    public static void login() {
        WebElement user = getElement("userName");
        WebElement pass = getElement("password");
        user.clear();
        user.sendKeys(readProperty("user"));
        pass.clear();
        pass.sendKeys(readProperty("user"));
        getElement("btn").submit();
    }

    public static WebElement getElement(String elem) {
        String str = readProperty(elem);
        WebElement element = null;
        if (str.matches("^[A-Za-z0-9\\-_.]*")) {
            waitTime(1);
            element = driver.findElement(By.id(str));
        } else if (str.matches("[a-z]{0,5}?[#|.][A-Za-z0-9\\-_.]*.*|.*[a-z]{0,5}?\\[[a-z]*='[A-Za-z0-9\\-_.]*'].*")) {
            waitTime(1);
            element = driver.findElement(By.cssSelector(str));
        } else if (str.matches("//\\*\\[@[a-z]*=\\W[A-Za-z0-9\\-_.]*\\W].*")) {
            waitTime(1);
            element = driver.findElement(By.xpath(str));
        } else {
            System.out.println("Locator is NULL");
        }
        return element;
    }

    private static String readProperty(String key) {
        Properties prop = new Properties();
        try {
            prop.load(new FileInputStream(new File("src\\test\\data\\dev.properties").getAbsolutePath()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return prop.getProperty(key);
    }

    public WebDriver getDriver() {
        return driver;
    }

    public void goHome() {
        getElement("home").click();
    }

    public void goHomeAndAddMovie() {
        if (!isDisplayed("addMovie")) {
            getElement("home").click();
        } else {
            getElement("addMovie").click();
        }
    }

    public void fillForm() {
        getElement("imdb").sendKeys("Silent Hill");
        getElement("search").submit();
        getElement("firstInSearch").click();
    }

    public void submitForm() {
        getElement("saveForm").submit();
    }

    public void searchFilm(String film) {
        WebElement search = getElement("homeSearch");
        search.clear();
        search.sendKeys(film);
        search.sendKeys(Keys.RETURN);
    }

    public void delete() {
        goHomeAndAddMovie();
        fillForm();
        submitForm();
        goHome();
        searchFilm("Silent Hill");
        if (isDisplayed("silentHill")) {
            while (isDisplayed("silentHill")) {
                getElement("silentHill").click();
                getElement("remove").click();
                getDriver().switchTo().alert().accept();
                goHome();
                waitTime(2);
            }
        }
    }

    private boolean isDisplayed(String locator) {
        boolean result;
        try {
            WebDriverWait wait = new WebDriverWait(getDriver(), 10);
            wait.until(ExpectedConditions.visibilityOf(getElement(locator)));
            result = true;
        } catch (Exception e) {
            result = false;
        }
        return result;
    }

    private static WebDriver.Timeouts waitTime(int time) {
        try {
            Thread.sleep(time*1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    }

}
